package com.lzw.breezeNovel.webApp.service.impl.novel;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.model.entity.pojo.novel.Book;
import com.lzw.breezeNovel.model.entity.pojo.novel.BookRanking;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.entity.query.SimplePage;
import com.lzw.breezeNovel.model.enums.novel.BookStatus;
import com.lzw.breezeNovel.webApp.mapper.novel.BookRatingMapper;
import com.lzw.breezeNovel.webApp.service.novel.BookRankingService;
import com.lzw.breezeNovel.webApp.vo.novel.BookVo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.novel.impl
 * @date 2025/3/3
 * @description 小说排行榜
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class BookRankingServiceImpl implements BookRankingService {

    /**
     * 注入小说排行榜的mapper
     */
    @Autowired
    private BookRatingMapper bookRatingMapper;

    /**
     * 获取小说排行榜
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<BookVo> getRanking() {
        //查询小说排行榜
        List<BookRanking> bookRatings = bookRatingMapper.selectList();
        List<BookVo> bookVos = getBookVos(bookRatings);
        return bookVos;
    }

    /**
     * 根据排行类型查询分页查询数据
     * @param bookRanking
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<BookVo> getRankingByPage(@NotNull BookRanking bookRanking) {
        //获取分页数据
        Integer page = Math.max(1, Optional.ofNullable(bookRanking.getPageNo()).orElse(1));
        Integer pageSize = Math.max(1, Optional.ofNullable(bookRanking.getPageSize()).orElse(10));
        //查询数据
        Integer count = bookRatingMapper.selectCount(bookRanking);
        //封装分页对象
        SimplePage simplePage = new SimplePage(page, pageSize, count);
        //计算分页
        simplePage.action();
        //判断是否有数据
        if(count == 0){
            return new PaginationResultQuery<>(0, pageSize, page, 0, Collections.emptyList());
        }
        bookRanking.setSimplePage(simplePage);
        //进行分页查询
        List<BookRanking> bookRatings = bookRatingMapper.selectListByPage(bookRanking);
        //获取小说
       List<BookVo> bookVos = getBookVos(bookRatings);
       //封装分页结果对象
        PaginationResultQuery<BookVo> ps = (PaginationResultQuery<BookVo>) new PaginationResultQuery<>(count, pageSize, simplePage.getPageNo(), bookVos.size(), bookVos);
        return ps;
    }

    /**
     * 获取循环遍历的小说
     *
     */
    private List<BookVo> getBookVos(@NotNull List<BookRanking> bookRatings) {
        //获取小说
        List<BookVo> bookVos = bookRatings.stream().map(bookRating -> {
            Book book = bookRating.getBook();
            //创建BookVo对象
            BookVo bookVo = new BookVo();
            //进行对象拷贝
            try {
                BeanUtils.copyProperties(book, bookVo);
                //获取小说的状态
                Integer status = book.getStatus();
                String statusDesc = BookStatus.getDescByCode(status);
                bookVo.setStatus(statusDesc);
                //设置小说排放
                bookVo.setTotalScore(book.getTotalScore());
                return bookVo;
            }catch (Exception e){
                log.error("对象拷贝失败",e);
                throw new BaseException(500,"拷贝对象时出现异常");
            }
        }).filter(bookVo -> bookVo != null).collect(toList());
        return bookVos;
    }



}
